/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.ui.dialogs;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.DataType;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Enumeration;
import org.eclipse.uml2.uml.InstanceSpecification;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PrimitiveType;

import com.cea.papyrus.core.PapyrusPlugin;


/**
 * 
 */
public class ElementImportTreeSelectionDialog extends Dialog {
	
	/**
	 * 
	 */
	protected Package model;
	
	/**
	 * 
	 */
	protected ArrayList<Element> elementsToImport = new ArrayList<Element>();
	
	/**
	 * 
	 */
	protected Tree elementTree;
	
	/**
	 * 
	 */
	protected static final Image IMG_PACKAGE = PapyrusPlugin.getImage(PapyrusPlugin.UML_ICONS_16x16+"Package.gif");
	
	/**
	 * 
	 */
	protected static final Image IMG_CLASS = PapyrusPlugin.getImage(PapyrusPlugin.UML_ICONS_16x16+"Class.gif");
	
	/**
	 * 
	 */
	protected static final Image IMG_DATATYPE = PapyrusPlugin.getImage(PapyrusPlugin.UML_ICONS_16x16+"DataType.gif");
	
	/**
	 * 
	 */
	protected static final Image IMG_ENUM = PapyrusPlugin.getImage(PapyrusPlugin.UML_ICONS_16x16+"Enumeration.gif");
	
	/**
	 * 
	 */
	protected static final Image IMG_PRIMITIVE = PapyrusPlugin.getImage(PapyrusPlugin.UML_ICONS_16x16+"PrimitiveType.gif");
	
	/**
	 * 
	 */
	protected static final Image IMG_ASSOCIATION = PapyrusPlugin.getImage(PapyrusPlugin.UML_ICONS_16x16+"Association.gif");
	
	/**
	 * 
	 */
	protected static final Image IMG_INSTANCESPEC = PapyrusPlugin.getImage(PapyrusPlugin.UML_ICONS_16x16+"InstanceSpecification.gif");

	
	/**
	 * 
	 * 
	 * @param model 
	 * @param parent 
	 */
	public ElementImportTreeSelectionDialog (Shell parent, Package model) {
		super (parent);
		this.model = model;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
	 */
	/**
	 * 
	 * 
	 * @param parent 
	 * 
	 * @return 
	 */
	protected Control createDialogArea(Composite parent){
		Composite comp=(Composite)super.createDialogArea(parent);
		elementTree = new Tree(comp, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL);
						
		TreeItem item = new TreeItem(elementTree, SWT.NONE);
		item.setText(model.getName());
		item.setData(model);
		item.setImage(IMG_PACKAGE);
		buildImportTreeList(item, model);
		
		elementTree.setLayoutData(new GridData(300, 300));
		
		elementTree.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				if (event.detail == SWT.CHECK) {
					if(elementsToImport.contains(event.item.getData()))
						elementsToImport.remove(event.item.getData());
					else
						elementsToImport.add((Element) event.item.getData());
				}
			}
		});
		
		return comp;
	}
	

	/**
	 * Returns the elements to import.
	 * 
	 * @return 
	 */
	public ArrayList getResult(){
		return elementsToImport;
	}
	
	/**
	 * This method builds the element tree widget containing all packageable elements of a given package.
	 * 
	 * @param _package the package to explore
	 * @param elemTree tree parent of packageable elements
	 */
	protected void buildImportTreeList(TreeItem elemTree, Package _package){
		Iterator elemIter = _package.getPackagedElements().iterator();
		while(elemIter.hasNext()) {
			Element elem = (Element) elemIter.next();
			if(elem instanceof Package){
				TreeItem item = new TreeItem(elemTree, SWT.NONE);
				item.setText(((Package)elem).getName());
				item.setData(elem);
				item.setImage(IMG_PACKAGE);
				buildImportTreeList(item, (Package)elem);
			}
			else if(elem instanceof NamedElement){
				TreeItem item = new TreeItem(elemTree, SWT.NONE);
				item.setText(((NamedElement)elem).getName());
				item.setData(elem);
				
				/* icon setting */
				if(elem instanceof Association)
					item.setImage(IMG_ASSOCIATION);
				else if(elem instanceof Enumeration)
					item.setImage(IMG_ENUM);
				else if(elem instanceof PrimitiveType)
					item.setImage(IMG_PRIMITIVE);
				else if(elem instanceof DataType)
					item.setImage(IMG_DATATYPE);
				else if(elem instanceof org.eclipse.uml2.uml.Class)
					item.setImage(IMG_CLASS);
				else if(elem instanceof InstanceSpecification)
					item.setImage(IMG_INSTANCESPEC);
			}
		}
	}

}
